using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using Rest;
using Rest.Transport;
  
namespace Coriander.Google.Services.Authentication
{
    /// <summary>
    /// 
    /// </summary>
    public class GoogleAccounts
    {
        // Sourceed from Google mail API on sourceforge
        /// <summary>
        /// Generates a <see cref="Cookie"/> for use with Gmail.
        /// </summary>
        /// <param name="cookieName">The name of the cookie to generate.</param>
        /// <returns>The <see cref="Cookie"/> that can be sent along with a <see cref="HttpWebRequest"/>.</returns>
        private Cookie GenerateCookie(String cookieName)
        {
            Cookie output = new Cookie("none", "");
            output.Path = "/";
            output.Domain = ".google.com";

            switch (cookieName)
            {
                case "GMAIL_LOGIN":
                    double jsGetTime = Math.Round(((TimeSpan)(DateTime.Now - new DateTime(1970, 1, 1))).TotalMilliseconds);
                    output.Name = "GMAIL_LOGIN";
                    output.Value = "T" + (jsGetTime - 11701) + "/" + (jsGetTime - 11425) + "/" + jsGetTime;
                    break;
                case "TZ":
                    string tz = Math.Abs(TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes).ToString();
                    output.Name = "TZ";
                    output.Value = tz;
                    break;

                default:
                    throw new ArgumentException("Cannot generate cookie. '" + cookieName + "' is not a registered cookie.");

            }

            return output;

        }

        /// <summary>
        /// Logs in to Google mail
        /// </summary>
        /// <param name="credential"></param>
        /// <returns>
        /// A set of cookies which can be used to identify subsequent
        /// requests.</returns>
        public CookieCollection Login(NetworkCredential credential)
        {
            RestCommand command = new RestCommand("https://www.google.com/accounts/ServiceLoginAuth");
            command.Transport = new PostRestTransport();
            
            // Expects the following cookies
            command.Cookies.Add(GenerateCookie("GMAIL_LOGIN"));
            command.Cookies.Add(GenerateCookie("TZ"));  
            
            //command.Parameters.Add("continue", "https://mail.google.com/mail");
            command.Parameters.Add("service", "mail");
            command.Parameters.Add("rm", "false");
            command.Parameters.Add("Email", credential.UserName);
            command.Parameters.Add("Passwd", credential.Password);
            command.Parameters.Add("null", "Sign+in");

            using (StringWriter writer = new StringWriter())
            {
                using (WebResponse response = command.Execute())
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;

                    return httpResponse.Cookies;
                }
            }
        }
    }
}
